package com.hyy.controller;

import com.hyy.bean.Cart;
import com.hyy.bean.MaskUser;
import com.hyy.service.CartService;
import com.hyy.service.serviceImpl.CartServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "InsertCartServlet",urlPatterns = "/insertCart")
public class InsertCartServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		req.setCharacterEncoding( "utf-8" );
		resp.setContentType( "text/html;charset=utf-8" );

		CartService cartService = new CartServiceImpl();
		MaskUser user = (MaskUser) req.getSession().getAttribute("user");
		Long mid = Long.parseLong(req.getParameter("mid"));
		Long num = Long.parseLong(req.getParameter("num"));
		if(user==null){
			resp.getWriter().print("<script>alert('请先登录！'); location='/jsp/login.jsp';</script>");
		}else{
			cartService.insert(new Cart(user.getUserid(),mid,num));
			req.getRequestDispatcher("/showDetail?id="+mid).forward(req,resp);
		}
	}


}
